﻿using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace SportMeetingPlanner.Models
{
    public class EntityUserManagerRepository : IUserRepository
    {
        private SportMeetingEntities _db = new SportMeetingEntities();

        public void CreateUser(User userToCreate)
        {
            _db.Users.Add(userToCreate);
            SaveChanges();
        }

        public void DeleteUser(string userName)
        {
            User u = GetUserByUserName(userName);
            _db.Users.Remove(u);
            SaveChanges();
        }

        public User GetUserByUserName(string userName)
        {
            return _db.Users.Find(userName);
        }

        public IEnumerable<User> GetAllUsers()
        {
            return _db.Users.ToList<User>();
        }

        public int SaveChanges()
        {
            return _db.SaveChanges();
        }

        public IEnumerable<User> GetAllUsersOrdered()
        {
            return _db.Users.OrderBy(u => u.UserName).ToList<User>();
        }

        public void Dispose()
        {
            _db.Dispose();
        }

        public void ModifyUser(User userToModify)
        {
            _db.Entry(userToModify).State = EntityState.Modified;
        }
    }
}